package im

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	v1 "go-websocket/api/im/v1"
	"go-websocket/internal/service"
	"go-websocket/utility"
	"golang.org/x/net/context"
	"strings"
)

type userCtl struct {}

var User = new(userCtl)





func (c *userCtl)Login (r *ghttp.Request) {

	var req *v1.UserLoginReq
	if err := r.Parse(&req); err != nil {
		g.Log().Error(context.TODO(), err)
		//返回错误信息
		r.Response.WriteExit(utility.JsonResult{
			Err: 1,
			Msg: err.Error(),
		})
	}

	if strings.TrimSpace(req.UserName) == "" || strings.TrimSpace(req.Password) == "" {
		r.Response.WriteExit(utility.JsonResult{
			Err: 1,
			Msg: "登录数据有误",
		})
	}

	if token, err := service.Login.UserLogin(r, req); err == nil && token != ""{
		r.Response.WriteExit(utility.JsonResult{
			Err: 0,
			Msg: "登录成功",
			Data: g.Map{
				"token": token,
			},
		})
	} else {
		// 登录错误
		r.Response.WriteExit(utility.JsonResult{
			Err: 1,
			Msg: "登录错误",
			Data: nil,
		})
	}




}
